<?php

/**
 * 数据报表成本中心
 * Cloud
 */
class Report extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('pagination');
        $this->load->model('bill_model');
        $this->load->model('goods_model');
        $this->load->model('customer_model');
        $this->load->model('user_model');
        $this->load->model('cost_center_model');
    }

    //成本中心页面
    public function center()
    {
        $customer_id = $this->session->customer_id;
        if ($this->session->customer_info['identity'] == 1) {
            $c_id = $customer_id;
        } elseif ($this->session->customer_info['identity'] == 2) {
            $c_id = $this->user_model->customer_id_to_parent_id($customer_id);;
        } elseif ($this->session->customer_info['identity'] == 3) {
            $c_id = $this->user_model->customer_id_to_admin_id($customer_id);;
        }


        //每月的销售数据
        $y = date('Y');
        $labels = $moneys = array();
        for ($i = 1; $i <= 12; $i++) {
            $labels[] = $i . '月';
            $s = $y . '-' . sprintf("%'.02d", $i) . '-' . '01';
            $e = date('Y-m-t', strtotime($y . '-' . sprintf("%'.02d", $i) . '-' . '01'));
            if (strtotime($s) > time()) {
                //$moneys[] = '0.00';
            } else {
                $sql = "SELECT
                    IFNULL(sum(num*price),0) as total
                FROM
                    app_cost_center AS c
                LEFT JOIN app_order AS o ON c.id = o.cost_center_id
                LEFT JOIN app_order_detail AS d ON o.id = d.order_id
                WHERE
                    1 = 1
                    and c.customer_id = $c_id
                    and o.status = 1 
                    and o.order_at >= '$s'
                    and o.order_at <= '$e'
                ";
                if ($center_id = $this->input->get('center')) {
                    $sql .= " and c.id = $center_id ";
                }
                $data1 = $this->db->query($sql)->row_array();
                $moneys[] = $data1['total'];
            }
        }

        $center_lists = $this->cost_center_model->query_cost_center_list($c_id);
        $current_center = '成本中心' . '采购总额';
        $lists[0] = '成本中心';
        if (count($center_lists)) {
            foreach ($center_lists as $center) {
                $lists[$center['id']] = $center['name'];
                if ($center_id = $this->input->get('center')) {
                    if ($center['id'] == $center_id) {
                        $current_center = $center['name'] . '采购总额';
                    }
                }
            }
        }


        //年份的销售数据
        if ($this->input->get('month')) {
            $current_month = $this->input->get('month');
            $time = strtotime($current_month . '-01');
            $s = date('Y-m-01', $time);
            $e = date('Y-m-t', $time);
        } else {
            $current_month = date('Y-m');
            $s = date('Y-m-01');
            $e = date('Y-m-t');
        }

        $sql = "SELECT
                    c.name,
                    IFNULL(sum(num*price),0) as value
                FROM
                    app_cost_center AS c
                LEFT JOIN app_order AS o ON c.id = o.cost_center_id
                LEFT JOIN app_order_detail AS d ON o.id = d.order_id
                WHERE
                    c.customer_id = $c_id
                    and o.status = 1 
                    and o.order_at >= '$s'
                    and o.order_at <= '$e'
                GROUP BY c.id
                ORDER BY 	sum(num*price) desc
                Limit 0,5
                ";
        $data1 = $this->db->query($sql)->result_array();
        $data1_desc = array();
        if (count($data1) > 0) {
            $data1_desc[$data1[0]['name']] = 'cqs-eecb58';
            $data1[0]['name'] =  $data1[0]['name'];
            $data1[0]['color'] = '#eecb58';
        }
        if (count($data1) > 1) {
            $data1_desc[$data1[1]['name']] = 'cqs-4c80a7';
            $data1[1]['name'] = $data1[1]['name'];
            $data1[1]['color'] = '#4c80a7';
        }
        if (count($data1) > 2) {
            $data1_desc[$data1[2]['name']] = 'cqs-69b2e7';
            $data1[2]['name'] = $data1[2]['name'];
            $data1[2]['color'] = '#69b2e7';
        }
        if (count($data1) > 3) {
            $data1_desc[$data1[3]['name']] = 'cqs-e85b3f';
            $data1[3]['name'] = '￥';
            $data1[3]['color'] = '#e85b3f';
        }
        if (count($data1) > 3) {
            $data1_desc[$data1[4]['name']] = 'cqs-f9a825';
            $data1[4]['name'] = '￥';
            $data1[4]['color'] = '#f9a825';
        }
        $total_sql = "SELECT
                            IFNULL(sum(num * price),0) as total
                        FROM
                            app_cost_center AS c
                        LEFT JOIN app_order AS o ON c.id = o.cost_center_id
                        LEFT JOIN app_order_detail AS d ON o.id = d.order_id
                        WHERE
                            c.customer_id = $c_id
                            and o.status = 1 
                            and o.order_at >= '$s'
                            and o.order_at <= '$e'
                        ";
        $total = $this->db->query($total_sql)->row_array();

        //导出
        $export_sql = "SELECT
                            c.name,
                            IFNULL(count(DISTINCT order_id),0) as orders,
                            IFNULL(sum(num),0) as num,
                            IFNULL(sum(num*price),0) as price,
                          '$current_month' as month
                        FROM
                            app_cost_center AS c
                        LEFT JOIN app_order AS o ON c.id = o.cost_center_id
                        LEFT JOIN app_order_detail AS d ON o.id = d.order_id
                        WHERE
                            c.customer_id = $c_id
                            and o.status = 1 
                            and o.order_at >= '$s'
                            and o.order_at <= '$e'
                        GROUP BY c.id
                        ORDER BY 	sum(num*price) desc
                        ";
        $table_data = $this->db->query($export_sql)->result_array();

        //采购单品数Top5
        $top5_sql = "SELECT
                        d.product_id,
                        sum(num) as num
                    FROM
                        app_cost_center AS c
                    LEFT JOIN app_order AS o ON c.id = o.cost_center_id
                    LEFT JOIN app_order_detail AS d ON o.id = d.order_id
                    WHERE
                        c.customer_id = $c_id
                        and o.status = 1 
                        and o.order_bn is not null
                        and o.order_at > '2018-01-01'
                      and o.order_at < '2018-12-31'
                      and d.product_id LIKE '%%'
                    GROUP BY d.product_id
                    ORDER BY sum(num) desc
                    Limit 0,5";
        $top5_data = $this->db->query($top5_sql)->result_array();

        $pids = $top5 = array();
        if (count($top5_data)) {
            foreach ($top5_data as $p) {
                $pids[] = $p['product_id'];
            }

            $admin_shop_user_id = $this->user_model->customer_id_to_admin_to_shop_user_id($customer_id);
            $goodsinfo = $this->goods_model->company_goods_infoes(implode(',', $pids),$admin_shop_user_id);

            if ($goodsinfo) {
                if (count($goodsinfo)) {
                    foreach ($top5_data as $v) {
                        foreach ($goodsinfo as $p) {
                            if ($p['goods_id'] == $v['product_id']) {
                                $p['num'] = $v['num'];
                                $top5[$p['goods_id']] = $p;
                                continue;
                            }
                        }
                    }
                }
            }
        }


        $this->load->view('report/center',
            array(
                'data1' => str_replace('\\\r\\\n', '\r\n', json_encode($data1, 320)),
                'data1_desc' => $data1_desc,
                'month_total' => $total['total'],
                'current_month' => $current_month,
                'labels' => json_encode($labels),
                'moneys' => json_encode($moneys),
                'table_data' => $table_data,
                'lists' => $lists,
                'center' => $this->input->get('center') ? $this->input->get('center') : 0,
                'current_center' => $current_center,
                'top5' => $top5
            )
        );
    }

}